#include "CRC.h"

static byte crc_table[256];     /* 8-bit table */
static boolean made_table=0;

/* Constructor */
CRC::CRC()
{
  int i,j;
  byte crc;
  
  if (!made_table)
  {
    for (i=0; i<256; i++)
    {
      crc = i;
      for (j=0; j<8; j++)
      {
        crc = (crc << 1) ^ ((crc & 0x80) ? DI : 0);
      }
      crc_table[i] = crc & 0xFF;
    }
    made_table=1;
  }
  else
  {
    /* Already initialised */
  }
  m_crcValue = 0x00;
}

 /* METHODES */

/**
* \brief Processing for switch rail evolution
* \param byte to compute
* \return new crc
*/
byte CRC::update(byte m)
{
  m_crcValue = crc_table[(m_crcValue) ^ m];
  m_crcValue &= 0xFF;
  return m_crcValue;
}
